<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use kartik\select2\Select2;
use yii\web\JsExpression;
use app\common\Functions;

/* @var $this yii\web\View */
/* @var $model \yii2mod\rbac\models\BizRuleModel */
/* @var $form ActiveForm */
?>

<?php

$fieldOptions  = [];
$labelOptions  = [];
$fieldOptions['class']  = 'col-md-6';
$options['options']=$fieldOptions;
$selectOptions = [];
$selectOptions['style'] = 'width:100%';
$selectOptions['class'] = ' select2 select2-hidden-accessible';

?>

<?php
$form = ActiveForm::begin(); ?>
    <div class="box-body">

        <?php  echo  $form->field($model, 'id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->id]); ?>

        <div class="col-md-3">
        <?php  echo  $form->field($model, 'cust_name')->label( $prodSpecName . '客户名称')->textInput(['maxlength' => 64]); ?>
        </div>

        <div class="col-md-3">
            <?php  echo $form->field($model, 'cust_source_id' )->label('客户来源')->dropDownList( $domainSources ); ?>
        </div>

        <div class="col-md-3">
        <?php  echo $form->field($model, 'cust_type_id'  )->label('客户类型')->dropDownList( $domainTypes ); ?>
        </div>



        <div  style="width: 100%;float: left;">
            <div class="col-md-4">
            <?php
            // province
            // 如果类型是区域的话展示联动列表
            echo $form->field($model, "province")->label('区域')->dropDownList( ArrayHelper::map($regionList, 'id', 'region_name'), [
                'prompt'=>'--请选择省--',

                'onchange'=>'
                            $(".form-group #cust-city").show();
                            $.post("'.yii::$app->urlManager->createUrl('prod/cust/site').'?parent_region_id="+$(this).val(),function(data){
                            $("select#cust-city").html(data);
                            });',
            ]);
          //  $regionStyle='display:none';
            $regionStyle ='display:show';
    //        if ($update)
    //        {
    //            $regionStyle ='display:show';
    //        }

            ?>
            </div>
            <div class="col-md-4">
            <?php
            echo $form->field($model, "city" )->label('三级区域')->textInput(['style'=>'display:none']) ->dropDownList( $model->getCityList($model->province),
                [
                    'prompt'=>'--请选择市--',
                    'style'=>$regionStyle,
                    'onchange'=>'
                            $(".form-group #cust-region_id").show();
                            $.post("'.yii::$app->urlManager->createUrl('prod/cust/site').'?parent_region_id="+$(this).val(),function(data){
                                $("select#cust-region_id").html(data);
                            });',
                ]);
            ?>
            </div>
            <div class="col-md-4">
            <?php
            echo $form->field($model, "region_id")->label('区域类型')->textInput(['style' => 'display:none;']) ->dropDownList( $model->getCityList($model->city),
                [
                    'prompt'=>'--请选择区、县--',
                    'style'=>$regionStyle,

                ]);

            ?>
        </div>
    </div>

    <?php

        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;
 
            // 文件
           if ($prodAttr->attr_spec_type_id==2)
           {
               $pluginOptions=[];
               $pluginOptions['initialPreviewAsData'] = true;
               $pluginOptions['showRemove'] = true;
               $pluginOptions['showUpload'] = false;

               if ($update)
               {
                   $pluginOptions['initialPreview'] = $p1[$prodAttr->attr_spec_id];
               }

               // echo $form->field($uploadModel, "file[]")->label($prodAttr->attr_spec_name)->widget(FileInput::classname(), [
               //attrSpec[{$prodAttr->attr_spec_id}][content]
               echo $form->field($uploadModel, "file[{$prodAttr->attr_spec_id}][]")->label($prodAttr->attr_spec_name)->widget(FileInput::classname(), [
                   'options' => ['multiple' => false],
                   'pluginOptions' =>$pluginOptions,
               ])->fileInput();
           }
           elseif ($prodAttr->attr_spec_type_id==3) // 如果type等于3 需要进行字符转义
           {  ?>
        <div class="col-md-4">
            <?php  echo $form->field($model, "localAttrsInsts[{$prodAttr->attr_spec_id}][content]")->label($prodAttr->attr_spec_name)->dropDownList(Functions::getDomains('attr_spec',$prodAttr->attr_spec_id));?>
        </div>

           <?php

           }
           else
           {
               ?>
               <div class="col-md-4">
                <?php  echo $form->field($model, "localAttrsInsts[{$prodAttr->attr_spec_id}][content]" )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);  ?>
            </div>

        <?php }

            echo $form->field($model, "localAttrsInsts[{$prodAttr->attr_spec_id}][attrSpecTypeID]",['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $prodAttr->attr_spec_type_id]);
        

        }


        ?>




        <div class="col-md-12">
        <?php


           echo  $form->field($model, 'remark')->label(  '客户备注')->textInput(['maxlength' => 256]);


        //echo $form->field($model, 'region_type_id')->label('区域类型')->textInput();
        ?>
        </div>

        <?php if (!$update): ?>
            <div class="col-md-6">
            <?php
            $orderStaffIDStyles = [];
            $orderStaffIDStyles['options'] = ['placeholder' => '请选择 ...'];
            $orderStaffIDStyles['pluginOptions'] = [
                'placeholder' => '查询 ...',
                'allowClear' => true,
                'minimumInputLength' => 1,
                'language' => [
                    'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
                ],
                'ajax' => [
                    'url' => '/prod/prodinst/seachorderstaff',
                    'dataType' => 'json',
                    'data' => new JsExpression('function(params) { return {q:params.term}; }')
                ],
                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                'templateResult' => new JsExpression('function(res) { 
                return res.text; }'),
                'templateSelection' => new JsExpression('function (res) { 
                        return res.text; }')];
                echo $form->field($model, 'orderStaffID')->label( '分配追踪人')->widget(Select2::classname(), $orderStaffIDStyles);
            ?>
            </div>

            <div class="col-md-12">
            <?php  echo  $form->field($model, 'orderRemark')->label( '订单描述')->textInput(['maxlength' => 64]); ?>
            </div>

            <?php

            foreach ($orderAttrSpecs as $attrSpec)
            {
                $orderAttr = $attrSpec->attrSpecs;

                // 文件
                if ($orderAttr->attr_spec_type_id==2)
                {
                    $pluginOptions=[];
                    $pluginOptions['initialPreviewAsData'] = true;
                    $pluginOptions['showRemove'] = true;
                    $pluginOptions['showUpload'] = false;

                    if ($update)
                    {
                        $pluginOptions['initialPreview'] = $p1[$orderAttr->attr_spec_id];
                    }

                    // echo $form->field($uploadModel, "file[]")->label($orderAttr->attr_spec_name)->widget(FileInput::classname(), [
                    //attrSpec[{$orderAttr->attr_spec_id}][content]
                    echo $form->field($uploadModel, "file[{$orderAttr->attr_spec_id}][]")->label($orderAttr->attr_spec_name)->widget(FileInput::classname(), [
                        'options' => ['multiple' => false],
                        'pluginOptions' =>$pluginOptions,
                    ])->fileInput();
                }
                elseif ($orderAttr->attr_spec_type_id==3) // 如果type等于3 需要进行字符转义
                {  ?>
                    <div class="col-md-6">
                        <?php  echo $form->field($orderModel, "localAttrsInsts[{$orderAttr->attr_spec_id}][content]")->label($orderAttr->attr_spec_name)->dropDownList(Functions::getDomains('attr_spec',$orderAttr->attr_spec_id));?>
                    </div>

                    <?php

                }
                elseif ($orderAttr->attr_spec_type_id==4) // 如果type等于4 说明是一组值
                {
                    ?>
                    <div class="col-md-6">
                        <?php  echo $form->field($orderModel, "localAttrsInsts[{$orderAttr->attr_spec_id}][content]")->label( '最小'. $orderAttr->attr_spec_name)->textInput(['maxlength' => 64]);?>
                    </div>
                    <div class="col-md-6">
                        <?php  echo $form->field($orderModel, "localAttrsInsts[{$orderAttr->attr_spec_id}][content2]")->label('最大'.$orderAttr->attr_spec_name)->textInput(['maxlength' => 64]);?>
                    </div>

                    <?php
                }
                else
                {
                    ?>
                    <div class="col-md-6">
                        <?php  echo $form->field($orderModel, "localAttrsInsts[{$orderAttr->attr_spec_id}][content]" )->label($orderAttr->attr_spec_name)->textInput(['maxlength' => 64]);  ?>
                    </div>

                <?php }

                echo $form->field($orderModel, "localAttrsInsts[{$orderAttr->attr_spec_id}][attrSpecTypeID]",['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $orderAttr->attr_spec_type_id]);


            }


            ?>




        <?php endif; ?>

    </div>

    <div class="form-group box-footer">
        <?php echo Html::submitButton($model->getIsNewRecord() ? Yii::t('region', 'Create') : Yii::t('region', 'Update'), [
            'class' => $model->getIsNewRecord() ? 'btn btn-success' : 'btn btn-primary', ]); ?>
        <?php echo Html::a(Yii::t('region', 'Cancel'), Yii::$app->request->getReferrer(), ['class' => 'btn btn-default']); ?>
    </div>



<?php ActiveForm::end(); ?>

